<layout name="Layout/layout" />
<div class="row">
	<div class="col-lg-12">
		<h1 class="page-header">考试收费规则</h1>
	</div>
	<!-- /.col-lg-12 -->
</div>
<!-- /.row -->
<div class="row">
	<div class="col-lg-12">
		<div class="panel panel-default">
			<div class="panel-body">
                <h2>已定义规则</h2>
                <table class="table">
                    <tr>
                        <th>ID</th>
                        <th>考生选择的专业情况</th>
                        <th>收取的费用</th>
                    </tr>
                    <foreach name="hasexistrules" item="v" key="k">
                        <tr>
                            <td>{$v['id']}</td>
                            <td>
                                <?php
                                if(is_array($v['majorids'])){
                                    echo getMajorNames(implode(',', $v['majorids']));
                                }else{
                                    echo getMajorNames($v['majorids']);
                                }
                                ?>
                            </td>
                            <td>
                                {$v['numofvalue']} 元
                            </td>
                        </tr>
                    </foreach>
                </table>
                <h2>新增或修改规则</h2>
                <table class="table">
                    <tr>
                        <th>选择的专业个数</th>
                        <th>考生选择的专业情况</th>
                        <th>收取的费用</th>
                        <th>操作</th>
                    </tr>
                    <foreach name="rule" item="v" key="k">
                        <?php foreach($v as $vo){ ?>
                        <tr>
                            <td examcenterid="{$examcenterid}">{$k}</td>
                            <td majors="<?php if(is_array($vo))echo implode(',', $vo);else echo $vo; ?>">
                                <?php
                                if(is_array($vo)){
                                    echo getMajorNames(implode(',', $vo));
                                }else{
                                    echo getMajorNames($vo);
                                }
                                ?>
                            </td>
                            <td class="form-inline">
                                <div class="input-group">
                                  <input type="text" class="form-control" placeholder="如：10.00" data-toggle="popover" data-trigger="focus" title="Dismissible popover" data-content="And here's some amazing content. It's very engaging. Right?"/>
                                  <div class="input-group-addon">元</div>
                                </div>
                            </td>
                            <td>
                                <button type="button" class="btn btn-default btn-save">保存</button>
                            </td>
                        </tr>
                        <?php } ?>
                    </foreach>
                </table>
			</div>
			<!-- /.panel-body -->
		</div>
		<!-- /.panel -->
	</div>
	<!-- /.col-lg-12 -->
</div>
<script>
	window.onload = function(){
        //$('[data-toggle="tooltip"]').tooltip();
        function checkInput(input){
            var value = input.val();
            if (/^[0-9]+\.[0-9]{2}$/.test(value)){
                input.parent().removeClass('has-error');
                input.parent().addClass('has-success');
                return true;
            }
            input.parent().removeClass('has-success');
            input.parent().addClass('has-error');
            return false;
        }
        $('input[type=text]').change(function(){
            checkInput($(this));
        });
        $('input[type=text]').keyup(function(){
            checkInput($(this));
        });
        $('.btn-save').click(function(){
            if(checkInput($(this).parent().prev().find('input'))){
                var majors = $(this).parent().siblings().eq(1).attr('majors');
                var examcenterid = $(this).parent().siblings().eq(0).attr('examcenterid');
                var money = $(this).parent().prev().find('input').val();
                $.post("{:U('Admin/Examcenter/saverule')}",{
                    majors: majors,
                    examcenterid: examcenterid,
                    money: money
                },function(data, state){
                    console.log(data);
                    window.location.href="{:U('Admin/Examcenter/rule')}?id=" + examcenterid;
                });
            }
        });
	}
</script>
